package com.hckjsec.cloud.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.hckjsec.cloud.domain.AppletsBusinessCost;
import com.hckjsec.cloud.domain.AppletsBusinessMatter;
import com.hckjsec.cloud.domain.wechatDataView.BidDataView;
import com.hckjsec.cloud.domain.wechatView.OpenidView;
import com.hckjsec.cloud.mapper.AppletsBusinessCostMapper;
import com.hckjsec.cloud.mapper.AppletsBusinessMatterMapper;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hckjsec.cloud.mapper.AppletsBusinessMapper;
import com.hckjsec.cloud.domain.AppletsBusiness;
import com.hckjsec.cloud.service.IAppletsBusinessService;

/**
 * 申办信息Service业务层处理
 * 
 * @author cyt
 * @date 2021-08-23
 */
@Service
public class AppletsBusinessServiceImpl implements IAppletsBusinessService 
{
    @Autowired
    private AppletsBusinessMapper appletsBusinessMapper;

    @Autowired
    private AppletsBusinessMatterMapper appletsBusinessMatterMapper;

    @Autowired
    private AppletsBusinessCostMapper appletsBusinessCostMapper;
    /**
     * 查询申办信息
     * 
     * @param id 申办信息ID
     * @return 申办信息
     */
    @Override
    public AppletsBusiness selectAppletsBusinessById(Long id)
    {
        return appletsBusinessMapper.selectAppletsBusinessById(id);
    }

    /**
     * 查询申办信息列表
     * 
     * @param appletsBusiness 申办信息
     * @return 申办信息
     */
    @Override
    public List<AppletsBusiness> selectAppletsBusinessList(AppletsBusiness appletsBusiness)
    {
        return appletsBusinessMapper.selectAppletsBusinessList(appletsBusiness);
    }

    /**
     * 新增申办信息
     * 
     * @param appletsBusiness 申办信息
     * @return 结果
     */
    @Override
    public int insertAppletsBusiness(AppletsBusiness appletsBusiness)
    {
        return appletsBusinessMapper.insertAppletsBusiness(appletsBusiness);
    }

    /**
     * 修改申办信息
     * 
     * @param appletsBusiness 申办信息
     * @return 结果
     */
    @Override
    public int updateAppletsBusiness(AppletsBusiness appletsBusiness)
    {
        return appletsBusinessMapper.updateAppletsBusiness(appletsBusiness);
    }

    /**
     * 批量删除申办信息
     * 
     * @param ids 需要删除的申办信息ID
     * @return 结果
     */
    @Override
    public int deleteAppletsBusinessByIds(Long[] ids)
    {
        return appletsBusinessMapper.deleteAppletsBusinessByIds(ids);
    }

    /**
     * 删除申办信息信息
     * 
     * @param id 申办信息ID
     * @return 结果
     */
    @Override
    public int deleteAppletsBusinessById(Long id)
    {
        return appletsBusinessMapper.deleteAppletsBusinessById(id);
    }

    /**
     * 根据手机端数据新增数据
     *
     * @param id 申办信息ID
     * @return 主键id
     */
    @Override
    public Long insertAppletsBusinessData(OpenidView openidView, BidDataView bidDataView)
    {
        AppletsBusiness appletsBusiness = new AppletsBusiness();
        appletsBusiness.setConfigId(openidView.getConfigId());
        appletsBusiness.setDeptId(openidView.getDeptId());
        appletsBusiness.setOpenid(openidView.getOpenId());
        appletsBusiness.setAreatype(StringUtils.isHcNull(openidView.getAreatype(),"1"));
        appletsBusiness.setBusinessid(bidDataView.getBusinessId());
        appletsBusiness.setUrgent(bidDataView.getUrgent());
        appletsBusiness.setAttestation(bidDataView.getAttestation());
        appletsBusiness.setMailname(StringUtils.isHcNull(bidDataView.getMail().getName(),""));
        appletsBusiness.setMailaddress(StringUtils.isHcNull(bidDataView.getMail().getAddress(),""));
        appletsBusiness.setMaildetailaddress(StringUtils.isHcNull(bidDataView.getMail().getDetailAddress(),""));
        appletsBusiness.setMailphone(StringUtils.isHcNull(bidDataView.getMail().getPhone(),""));
        appletsBusiness.setCategory(StringUtils.isHcNull(bidDataView.getCategory().getKey(),""));
        appletsBusiness.setCategoryname(StringUtils.isHcNull(bidDataView.getCategory().getValue(),""));
        appletsBusiness.setPurpose(StringUtils.isHcNull(bidDataView.getPurpose().getKey(),""));
        appletsBusiness.setPurposename(StringUtils.isHcNull(bidDataView.getPurpose().getValue(),""));
        appletsBusiness.setPlace(StringUtils.isHcNull(bidDataView.getPlace().getKey(),""));
        appletsBusiness.setPlacename(StringUtils.isHcNull(bidDataView.getPlace().getValue(),""));
        appletsBusiness.setLanguage(StringUtils.isHcNull(bidDataView.getLanguage().getKey(),""));
        appletsBusiness.setLanguagename(StringUtils.isHcNull(bidDataView.getLanguage().getValue(),""));
        appletsBusiness.setAppointednotary(StringUtils.isHcNull(bidDataView.getAppointedNotary().getKey(),""));
        appletsBusiness.setAppointednotaryphone(StringUtils.isHcNull(bidDataView.getAppointedNotary().getValue(),""));
        appletsBusiness.setAllparty(openidView.getRealName());
        appletsBusiness.setState("1");
        Long business_id=-1L;
        int rows = appletsBusinessMapper.insertAppletsBusiness(appletsBusiness);
        if (rows > 0){
            business_id=appletsBusiness.getId();
        }
        return business_id;
    }

    /**
     * 业务主表，修改总费用和总事项
     *
     * @param id 申办信息ID
     * @return 主键id
     */
    @Override
    public void updateCostAndMatter(Long id){
        List<String> matterNameList=new ArrayList<>();
        List<AppletsBusinessMatter> appletsBusinessMatterList=appletsBusinessMatterMapper.selectAppletsBusinessMatterList(new AppletsBusinessMatter(id));
        appletsBusinessMatterList.forEach(kv->{
            if(!matterNameList.contains(kv.getMattername())){
                matterNameList.add(kv.getMattername());
            }
        });
        BigDecimal totalAmount = new BigDecimal(0);
        List<AppletsBusinessCost> appletsBusinessCostList=appletsBusinessCostMapper.selectAppletsBusinessCostList(new AppletsBusinessCost(id));
        for (int i = 0; i < appletsBusinessCostList.size() ; i++) {
            totalAmount=totalAmount.add(appletsBusinessCostList.get(i).getTotalmoney());
        }
        AppletsBusiness appletsBusiness=appletsBusinessMapper.selectAppletsBusinessById(id);
        appletsBusiness.setAllmatter(StringUtils.join(matterNameList.toArray(),","));
        appletsBusiness.setTotalmoney(totalAmount);
        appletsBusinessMapper.updateAppletsBusiness(appletsBusiness);
    }
    /**
     * 根据案件状态对应的案件数量
     *
     * @param deptId 申办信息
     * @return 结果
     */
    @Override
    public List<Map<String, Integer>> findStateByNumMap(Long deptId) {
        return appletsBusinessMapper.findStateByNumMap(deptId);
    }
    /**
     * 根据公证事项对应的案件数量
     *
     * @param deptId 申办信息
     * @return 结果
     */
    @Override
    public List<Map<String, Integer>> findCategoryByNumMap(Long deptId) {
        return appletsBusinessMapper.findCategoryByNumMap(deptId);
    }

    /**
     * 根据日期对应的案件数量
     *
     * @param deptId 申办信息
     * @return 结果
     */
    @Override
    public List<Map<String, Integer>> findOperatortimeByNumMap(Long deptId) {
        return appletsBusinessMapper.findOperatortimeByNumMap(deptId);
    }

    /**
     * 根据案件状态各机构对应的案件数量
     *
     * @param deptId 申办信息
     * @return 结果
     */
    @Override
    public List<Map<String, Integer>> findDeptIdByNumMap(AppletsBusiness appletsBusiness) {
        return appletsBusinessMapper.findDeptIdByNumMap(appletsBusiness);
    }

    /**
     * 根据案件状态各机构对应的费用总额
     *
     * @param deptId 申办信息
     * @return 结果
     */
    @Override
    public List<Map<String, BigDecimal>> findDeptIdByMoneyMap(AppletsBusiness appletsBusiness) {
        return appletsBusinessMapper.findDeptIdByMoneyMap(appletsBusiness);
    }

    /**
     * 分组查询各类别数量
     *
     * @param ids 需要查询的数据ID
     * @return 结果
     */
    @Override
    public List<Map<String, Integer>> findCategoryIdByNumMap(Long[] ids) {
        return appletsBusinessMapper.findCategoryIdByNumMap(ids);
    }

    /**
     * 分组查询各月数量
     *
     * @param ids 需要查询的数据ID
     * @return 结果
     */
    @Override
    public List<Map<String, Integer>> findOperatortimeIdByNumMap(Long[] ids) {
        return appletsBusinessMapper.findOperatortimeIdByNumMap(ids);
    }

    /**
     * 查询申办信息列表2
     *
     * @param appletsBusiness 申办信息2
     * @return 申办信息集合2
     */
    @Override
    public List<AppletsBusiness> selectAdministrationBusinessList(AppletsBusiness appletsBusiness) {
        return appletsBusinessMapper.selectAdministrationBusinessList(appletsBusiness);
    }


    /**
     * 查询申办信息列表3
     *
     * @param appletsBusiness 申办信息2  类别
     * @return 申办信息集合2
     */
    @Override
    public List<AppletsBusiness> selectAdministrationCategoryList(AppletsBusiness appletsBusiness) {
        return appletsBusinessMapper.selectAdministrationCategoryList(appletsBusiness);
    }

    /**
     * 查询申办信息列表4
     *
     * @param appletsBusiness 申办信息 openid数组
     * @return 申办信息集合4
     */
    @Override
    public List<AppletsBusiness> selectAppletsBusinessOrderList(String[] openid) {
        return appletsBusinessMapper.selectAppletsBusinessOrderList(openid);
    }

    /**
     * 查询申办信息列表
     *
     * @param appletsBusiness 过滤开视频的案件
     * @return 申办信息集合
     */
    @Override
    public List<AppletsBusiness> selectAppletsBusinessVideoList(AppletsBusiness appletsBusiness) {
        return appletsBusinessMapper.selectAppletsBusinessVideoList(appletsBusiness);
    }
}
